//
//  DevicesViewController.m
//  iRMAP
//
//  Created by Ryan Enguero on 4/23/14.
//  Copyright (c) 2014 engueroryan. All rights reserved.
//

#import "DevicesViewController.h"

#import "DevicesCell.h"

#import "UiManager.h"

#import "ParserClass.h"

#import "Devices.h"

#import "CateTableCell.h"

#import "SubCateViewController.h"


@interface DevicesViewController () <UIFolderTableViewDelegate>

@property (strong, nonatomic) SubCateViewController *subVc;
@property (strong, nonatomic) NSDictionary *currentCate;


@end

@implementation DevicesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"Devices", @"Devices");
        
    }
    return self;
}

-(NSArray *)cates
{
    if (_cates == nil){
        
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"Category" withExtension:@"plist"];
        _cates = [NSArray arrayWithContentsOfURL:url];
        
    }
    
    return _cates;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.tableView setScrollEnabled:NO];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tmall_bg_furley.png"]];
        self.view.backgroundColor = [UIColor blackColor];
    self.navigationItem.title = @"AAAA";
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [[[ParserClass sharedItems] deviceDetails] count];
    
    //self.cates.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    static NSString *CellIdentifier = @"cate_cell";
    //
    //    CateTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //
    //    if (cell == nil) {
    //        cell = [[CateTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle
    //                                     reuseIdentifier:CellIdentifier];
    //        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //
    //    }
    //
    //    NSDictionary *cate = [self.cates objectAtIndex:indexPath.row];
    //    cell.logo.image = [UIImage imageNamed:[[cate objectForKey:@"imageName"] stringByAppendingString:@".png"]];
    //    cell.title.text = [cate objectForKey:@"name"];
    //
    //    NSMutableArray *subTitles = [[NSMutableArray alloc] init];
    //    NSArray *subClass = [cate objectForKey:@"subClass"];
    //    for (int i=0; i < MIN(4,  subClass.count); i++) {
    //        [subTitles addObject:[[subClass objectAtIndex:i] objectForKey:@"name"]];
    //    }
    //    cell.subTtile.text = [subTitles componentsJoinedByString:@"/"];
    //   // [subTitles release];
    //
    //    return cell;
    
    static NSString *CellIdentifier = @"Cell";
    
    DevicesCell *cell = (DevicesCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DevicesCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    // Configure the cell...
    
    Devices *devices = [[ParserClass sharedItems] deviceDetails][indexPath.row];
    
    cell.nameCaption.text = [devices deviceName];
    //  cell.tempCaption.text =
    cell.timeStampCaption.text = [NSString stringWithFormat:@"%@",[NSDate date]];
    
    
    return cell;
    
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SubCateViewController *subVc = [[SubCateViewController alloc]
                                    initWithNibName:NSStringFromClass([SubCateViewController class])
                                    bundle:nil];
    NSDictionary *cate = [self.cates objectAtIndex:indexPath.row];
    subVc.subCates = [cate objectForKey:@"subClass"];
    self.currentCate = cate;
    subVc.cateVC = self;
    
    self.tableView.scrollEnabled = NO;
    UIFolderTableView *folderTableView = (UIFolderTableView *)tableView;
    [folderTableView openFolderAtIndexPath:indexPath WithContentView:subVc.view
                                 openBlock:^(UIView *subClassView, CFTimeInterval duration, CAMediaTimingFunction *timingFunction){
                                     // opening actions
                                 }
                                closeBlock:^(UIView *subClassView, CFTimeInterval duration, CAMediaTimingFunction *timingFunction){
                                    // closing actions
                                }
                           completionBlock:^{
                               // completed actions
                               self.tableView.scrollEnabled = YES;
                           }];
    
}

-(CGFloat)tableView:(UIFolderTableView *)tableView xForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(void)subCateBtnAction:(UIButton *)btn
{
    
    NSDictionary *subCate = [[self.currentCate objectForKey:@"subClass"] objectAtIndex:btn.tag];
    NSString *name = [subCate objectForKey:@"name"];
    UIAlertView *Notpermitted=[[UIAlertView alloc] initWithTitle:@"AAAA"
                                                         message:[NSString stringWithFormat:@"BB:%@, ID: %@", name, [subCate objectForKey:@"classID"]]
                                                        delegate:nil
                                               cancelButtonTitle:@"APO"
                                               otherButtonTitles:nil];
    [Notpermitted show];
    // [Notpermitted release];
}
@end
